use diesel::RunQueryDsl;

use crate::model::schema::models;
use crate::model::schema::models::Post;
use crate::model::schema::schema::posts::dsl;

#[cfg(not(windows))]
const EOF: &str = "CTRL+D";

#[cfg(windows)]
const EOF: &str = "CTRL+Z";

struct PostSrv {
    repo: diesel::PgConnection,
}

impl PostSrv {
    fn search_page(&mut self, rec: models::Post) -> Vec<Post> {
        // let vec = dsl::posts.filter(
        //     dsl::published.eq(true)
        let vec = dsl::posts
            .load::<models::Post>(&mut self.repo)
            .expect("TODO: panic message");
        // vec.iter().for_each(|x| println!("{:?}", x));
        for post in vec.iter() {
            println!("{:?}", post);
        }
        return vec;
    }
}

#[cfg(test)]
mod crud {
    use crate::model::repo;
    use crate::model::schema::models;
    use crate::model::srv::post::PostSrv;

    #[test]
    fn char() {
        let mut srv = PostSrv {
            repo: repo::repo::establish_connection(),
        };
        let vec = srv.search_page(models::Post {
            id: 0,
            title: "".to_string(),
            body: "".to_string(),
            published: false,
            status: false,
        });
        println!("{:?}", vec);
    }
}
